/**
 * 
 */
package com.ntv.persistent.content;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import com.ntv.domain.IContent;
import com.ntv.persistent.AbstractDao;
import com.ntv.persistent.PersistentException;
import com.ntv.service.news.impl.NewsAppService;

/**
 * @author Vedamoorthi Vairam
 * @since 07/01/2009
 *
 */
public class SqlContentDao extends AbstractDao implements IContentDao {
	private static final Logger logger = Logger.getLogger(SqlContentDao.class);
	interface CONST 
	{
		interface QUERY 
		{
			final String GET_CONENT_BY_ID = "SELECT content_id, content, content_type,  content_size," +
					" created_by, created_on, updated_by, updated_on  FROM content WHERE content_id=:contentId";
			
			final String GET_CONENT_BY_WISH_ID = "SELECT image" +
			"  FROM wishes WHERE id=:id";
			
			final String GET_CONENT_BY_OBITUARY_ID = "SELECT image" +
			"  FROM obituary WHERE id=:id";
		}
	}

	public IContent create(IContent content) throws PersistentException
	{
		logger.info("Content table insertion.");
		// Content table insertion.
		Map<String, Object> parameters = null;
		// Inserting data into content table.
		SimpleJdbcInsert insertTempalte 
			= new SimpleJdbcInsert(getJdbcTemplate())
				.withTableName(getTableName())
				.usingGeneratedKeyColumns("content_id");
		
		parameters = new HashMap<String, Object>(10);
		
//		logger.info("content_size"+content.getContentSize());
//		logger.info("created_by"+content.getCreatedBy());
//		logger.info("created_on"+content.getCreatedOn());
//		logger.info("updated_by"+content.getUpdatedBy());
//		logger.info("updated_on"+content.getUpdatedOn());
//		logger.info("caption"+content.getCaption());
//		logger.info("content"+content.getContent());		
		
		parameters.put("content_size",content.getContentSize());
		parameters.put("created_by",content.getCreatedBy());
		parameters.put("created_on",content.getCreatedOn());
		parameters.put("updated_by",content.getUpdatedBy());
		parameters.put("updated_on",content.getUpdatedOn());
		parameters.put("caption",content.getCaption());
		parameters.put("content",content.getContent());
		
		parameters.put("content_type",content.getContentType());
		
		Number contentId = insertTempalte.executeAndReturnKey(parameters);
		content.setContentId(contentId.longValue());
		
		logger.info("Content table insertion completed successfully.");
		
		return content;
	}

	public IContent findConent(Long contentId) throws PersistentException
	{
		try {
			SqlParameterSource namedParameters = new MapSqlParameterSource("contentId", contentId);
			return (IContent)getNamedParameterJdbcTemplate().queryForObject(CONST.QUERY.GET_CONENT_BY_ID, namedParameters, getRowMapper());
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
		
	}
		
	public InputStream findWishesContent(Long wishId)  throws PersistentException {
		try {
			SqlParameterSource namedParameters = new MapSqlParameterSource("id", wishId);	
			InputStream is = (InputStream)getNamedParameterJdbcTemplate().queryForObject(CONST.QUERY.GET_CONENT_BY_WISH_ID, namedParameters, new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					return rs.getBinaryStream("image");
				}
			});
			return is;
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
	
	public InputStream findObituaryContent(Long wishId)  throws PersistentException {
		try {
			
			SqlParameterSource namedParameters = new MapSqlParameterSource("id", wishId);	
			InputStream is = (InputStream)getNamedParameterJdbcTemplate().queryForObject(CONST.QUERY.GET_CONENT_BY_OBITUARY_ID, namedParameters, new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					return rs.getBinaryStream("image");
				}
			});
			return is;
		}catch (DataAccessException ex) {
			throw new PersistentException(ex);
		}
	}
	
}
